package org.scohen.juploadr.ui;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.tags.Tag;

public class TagComponent extends Canvas {
    private Tag tag;
    private List<ImageAttributes> appliesTo;
    private Point size;

    public TagComponent(Composite parent, Tag t) {
        super(parent, SWT.NONE);
        this.tag = t;
        addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                TagComponent.this.paintControl(e);
            }
        });

        addMouseTrackListener(new MouseTrackAdapter() {
            @Override
            public void mouseEnter(MouseEvent e) {

            }
        });
    }

    public void addImageAttributes(List<ImageAttributes> l) {
        appliesTo.addAll(l);
    }

    public void addImageAttributes(ImageAttributes a) {
        appliesTo.add(a);
    }

    @Override
    public Point getSize() {
        return size;
    }

    private void paintControl(PaintEvent e) {
        GC gc = e.gc;
        Point textSize = gc.textExtent(tag.toDisplayString());
        Point tagSize = new Point(textSize.x + 10, textSize.y + 5);
        if (size == null) {
            size = new Point(textSize.x + 25, textSize.y + 2);
        }
        gc.setAdvanced(true);
        Color background = new Color(e.display, 255, 252, 203);
        Color outline = new Color(e.display, 190, 190, 190);
        Color black = new Color(e.display,0,0,0);
        gc.setBackground(background);
        int[] points = {tagSize.x, 0 ,tagSize.x+ 7, tagSize.y/2 , tagSize.x,tagSize.y};
        gc.fillPolygon(points);
        gc.fillRectangle(0,0,tagSize.x,tagSize.y);
        
        gc.setForeground(outline);
        // top
        gc.drawLine(0, 0, tagSize.x, 0);
        // left
        gc.drawLine(0, 0, 0, tagSize.y);
        // right
        gc.drawLine(0, tagSize.y, tagSize.x, tagSize.y);

        gc.drawLine(tagSize.x, 0, tagSize.x + 7, tagSize.y / 2);
        gc.drawLine(tagSize.x, tagSize.y, tagSize.x + 7, tagSize.y / 2);
        gc.drawOval(tagSize.x, tagSize.y / 2, 2, 2);
        
        gc.setForeground(black);
        gc.drawString(tag.toDisplayString(), 5, 3);
        
        background.dispose();
        outline.dispose();
        black.dispose();
    }
}
